/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sap.jco.client;

import com.sap.conn.jco.AbapException;
import com.sap.conn.jco.JCoDestination;
import com.sap.conn.jco.JCoDestinationManager;
import com.sap.conn.jco.JCoFunction;
import com.sap.conn.jco.JCoStructure;
import com.sap.conn.jco.JCoTable;
import com.sap.conn.jco.ext.DestinationDataEventListener;
import com.sap.conn.jco.ext.DestinationDataProvider;
import java.util.Properties;

public class CustomDestinationDataProvider
{
    static class MyDestinationDataProvider implements DestinationDataProvider
    {
        private DestinationDataEventListener eL;
        private Properties ABAP_AS_properties;         
        public Properties getDestinationProperties(String destinationName)
        {
            if(destinationName.equals("ABAP_AS") && ABAP_AS_properties!=null)
                return ABAP_AS_properties;
            
            return null;
        }

        public void setDestinationDataEventListener(DestinationDataEventListener eventListener)
        {
            this.eL = eventListener;
        }

        public boolean supportsEvents()
        {
            return true;
        }
        
        void changePropertiesForABAP_AS(Properties properties)
        {
            if(properties==null)
            {
                ABAP_AS_properties = null;
                eL.deleted("ABAP_AS");
            }
            else 
            {
                if(ABAP_AS_properties==null || !ABAP_AS_properties.equals(properties))
                {
	                ABAP_AS_properties = properties;
                    eL.updated("ABAP_AS");
                }
            }
        }
    }
    
    public static void main(String[] args) throws Exception
    {
        
        JCoTable codes = CustomDestinationDataProvider.getCompany();
        
        if(codes == null) 
            System.out.println(" codes is null ");
        
        for (int i = 0; i < codes.getNumRows(); i++) 
        {
            codes.setRow(i);
            System.out.println(codes.getString("COMP_CODE") + '\t' + codes.getString("COMP_NAME"));
        }
        
    }
    
    public static JCoTable getCompany(){
        try{
            Properties connectProperties = new Properties();
            connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, SAPClientInfo.JcoAsHost());
            connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR,  SAPClientInfo.JcoSysNr());
            connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, SAPClientInfo.JcoClient());
            connectProperties.setProperty(DestinationDataProvider.JCO_USER,   SAPClientInfo.JcoUser());
            connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, SAPClientInfo.JcoPasswd());
            connectProperties.setProperty(DestinationDataProvider.JCO_LANG, SAPClientInfo.JcoLang());

            MyDestinationDataProvider myProvider = new MyDestinationDataProvider();

            com.sap.conn.jco.ext.Environment.registerDestinationDataProvider(myProvider);
            myProvider.changePropertiesForABAP_AS(connectProperties);
            
        }catch(Exception ex)
        {
            System.out.println("Destination : " + ex.toString());
        }
        
        try{
//        JCoDestination ABAP_AS = JCoDestinationManager.getDestination("ABAP_AS");
//        ABAP_AS.ping();
//        System.out.println("ABAP_AS destination is ok");        
        
        //============================================================================================
        JCoDestination destination = JCoDestinationManager.getDestination("ABAP_AS");
        JCoFunction function = destination.getRepository().getFunction("BAPI_COMPANYCODE_GETLIST");
        if(function == null)
            throw new RuntimeException("BAPI_COMPANYCODE_GETLIST not found in SAP.");
        
        try
        {
            function.execute(destination);
        }
        catch(AbapException e)
        {
            System.out.println("Destination : " + e.toString());
            return null;
        }
        
        
        JCoStructure returnStructure = function.getExportParameterList().getStructure("RETURN");
        if (!(returnStructure.getString("TYPE").equals("")||returnStructure.getString("TYPE").equals("S")))   
        {
           throw new RuntimeException(returnStructure.getString("MESSAGE"));
        }
        
        JCoTable codes = function.getTableParameterList().getTable("COMPANYCODE_LIST");              
        if(!codes.isEmpty())
            return codes;
        else 
            return null;
        
        }catch(Exception ex)
        {
            System.out.println(" Exception : " + ex.getMessage());
        }
        return null;
    }
}
